<?php

namespace Modules\Good\Models;
use Modules\Category\Models\Category;
use Modules\Auth\Models\User;
use Illuminate\Database\Eloquent\Model;
use Modules\Comment\Models\Comment;
class Good extends Model
{
    protected $table = 'goods'; // 表名
    protected $fillable = [
        'user_id', 'category_id', 'title', 'description', 'price', 
        'stock', 'cover', 'pics', 'detail'
    ]; // 可批量赋值字段

    // 转换pics字段为JSON
    protected $casts = [
        'pics' => 'array'
    ];
    // 关联分类
    public function category()
    {
        return $this->belongsTo(Category::class,'category_id','id');
    }
    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }
    // 关联评论
    public function comments()
    {
        return $this->hasMany(Comment::class,'good_id','id');
    }
}
